﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Utils
{
    public static class Extensions
    {
        #region Dictionary
        public static Tvalue TryGet<Tkey, Tvalue>(this Dictionary<Tkey, Tvalue> dic, Tkey key)
        {
            Tvalue value;
            dic.TryGetValue(key, out value);
            return value;
        }

        #endregion

        #region String
        public static int ToInt(this string str)
        {
            int id;
            int.TryParse(str, out id);//这里当转换失败时返回的id为0
            return id;
        }
        #endregion

        /// <summary>
        /// 将string转化为nList 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static List<int> ToNList(this string str)
        {
            if (str == null) return null;
            List<int> list = new List<int>();
            string[] strArr = str.Split(',');
            for (int i = 0; i < strArr.Length; i++)
            {
                list.Add(int.Parse(strArr[i]));
            }
            return list;
        }

        public static List<Char> ToCharList(this string str)
        {
            if (str == null) return null;
            List<Char> list = new List<Char>();
            string answer = str.Replace(",", "");
            Char[] charArray = answer.ToCharArray();
            list = charArray.ToList();
            return list;
        }
        public static List<string> ToStrList(this string str)
        {
            if (str == null) return null;
            List<string> list = new List<string>();
            string[] strArr = str.Split(',');

            for (int i = 0; i < strArr.Length; i++)
            {
                int number;
                bool b = int.TryParse(strArr[i], out number);
                if (b)
                {
                    if (1 <= number && 36 >= number)
                    {
                        int num = number + 64;
                        System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                        byte[] btNumber = new byte[] { (byte)num };
                        strArr[i] = asciiEncoding.GetString(btNumber);
                    }
                }

            }
            return strArr.ToList();
        }

        /// <summary>
        /// 将nList转化为string
        /// </summary>
        /// <param name="nList"></param>
        /// <returns></returns>
        public static string NListToString(this List<int> nList)
        {
            string str = "";
            foreach (var item in nList)
            {
                str += item + ",";
            }
            if (str.Length > 1)
                str = str.Remove(str.Length - 1, 1);
            return str;
        }

        public static string StrListToString(this List<string> strList)
        {
            string str = "";
            foreach (var item in strList)
            {
                str += item + "\r";
            }
            return str;
        }



    }
}
